package view;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import model.AdminControlMessage;

public class AdminPanel extends JPanel implements ActionListener {

	private ClientFrame frame;
	private JLabel addCandidateLabel;
	private JButton backButton;
	private JButton startEndVoteButton;
	private JButton addButton;
	private JTextField newCandidateTextField;
	private JTextArea candidatesListTextArea;
	private JScrollPane scrollingArea;
	public Object[] candidatesListArray;

	public AdminPanel(ClientFrame f) {
		frame = f;
		this.setBounds(0, 0, frame.getWidth(), frame.getHeight());
		this.setBackground(Color.WHITE);
		this.setLayout(null);

		addCandidateLabel = new JLabel("Add candidate:");
		addCandidateLabel.setBounds(300, 150, 100, 20);
		this.add(addCandidateLabel);

		newCandidateTextField = new JTextField();
		newCandidateTextField.setBounds(390, 150, 120, 20);
		this.add(newCandidateTextField);

		addButton = new JButton("Add");
		addButton.setBounds(300, 180, 90, 20);
		addButton.addActionListener(this);
		this.add(addButton);

		candidatesListTextArea = new JTextArea();
		this.updateCandidatesList();
		candidatesListTextArea.setEditable(false);
		candidatesListTextArea.setWrapStyleWord(true);
		scrollingArea = new JScrollPane(candidatesListTextArea);
		scrollingArea.setBounds(300, 205, 200, 200);
		this.add(scrollingArea);

		backButton = new JButton("Back");
		backButton.setBounds(410, 410, 90, 20);
		backButton.addActionListener(this);
		// this.add(backButton);

		startEndVoteButton = new JButton();

		startEndVoteButton.setBounds(300, 410, 90, 20);
		startEndVoteButton.addActionListener(this);
		this.add(startEndVoteButton);

		try {
			Socket connectionSocket = new Socket("127.0.0.1", 4444);
			ObjectOutputStream objectOut = new ObjectOutputStream(
					connectionSocket.getOutputStream());
			ObjectInputStream objectIn = new ObjectInputStream(connectionSocket
					.getInputStream());

			AdminControlMessage adminControlMessage = new AdminControlMessage();
			adminControlMessage.setMessage("get voting status");
			adminControlMessage.setUserSession(frame.getUserSession());
			objectOut.writeObject(adminControlMessage);

			objectOut.flush();
			String message = (String) objectIn.readObject();

			if (message.equals("Voting hasn't started")) {
				startEndVoteButton.setText("Start vote");
			} else if (message.equals("Voting has started")) {
				this.addButton.setEnabled(false);
				this.newCandidateTextField.setEnabled(false);
				startEndVoteButton.setText("End vote");
			}

		} catch (Exception ex) {
			ex.printStackTrace();
		}

		this.repaint();

		frame.setContentPane(this);

	}

	@SuppressWarnings("unchecked")
	private void updateCandidatesList() {
		try {
			Socket connectionSocket = new Socket("127.0.0.1", 4444);
			ObjectOutputStream objectOut = new ObjectOutputStream(
					connectionSocket.getOutputStream());
			ObjectInputStream objectIn = new ObjectInputStream(connectionSocket
					.getInputStream());

			AdminControlMessage adminControlMessage = new AdminControlMessage();
			adminControlMessage.setMessage("get candidates list");
			adminControlMessage.setUserSession(frame.getUserSession());
			objectOut.writeObject(adminControlMessage);
			objectOut.flush();
			candidatesListArray = ((List<String>) objectIn.readObject())
					.toArray();

		} catch (Exception ex) {
			ex.printStackTrace();
		}

		String candidates = "";
		for (int x = 0; x < candidatesListArray.length; x++) {
			candidates += x + 1 + ") " + candidatesListArray[x] + "\n";
		}

		candidatesListTextArea.setText(candidates);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == backButton) {

		} else if (e.getSource() == addButton) {

			if (newCandidateTextField.getText().equals("")) {
				JOptionPane.showMessageDialog(this,
						"Please enter the name of the candidate !!");
			} else if (candidatesListArray.length > 4) {
				JOptionPane.showMessageDialog(this,
						"Sorry you cannot add more than 5 clients !!");
			} else {
				try {
					Socket connectionSocket = new Socket("127.0.0.1", 4444);
					ObjectOutputStream objectOut = new ObjectOutputStream(
							connectionSocket.getOutputStream());
					ObjectInputStream objectIn = new ObjectInputStream(
							connectionSocket.getInputStream());

					AdminControlMessage adminControlMessage = new AdminControlMessage();
					adminControlMessage.setMessage("add:"
							+ newCandidateTextField.getText());
					adminControlMessage.setUserSession(this.frame
							.getUserSession());
					objectOut.writeObject(adminControlMessage);
					objectOut.flush();
					String message = (String) objectIn.readObject();
					if (message.equals("Candidate added")) {
						this.updateCandidatesList();
					}

				} catch (Exception ex) {
					ex.printStackTrace();
				}

			}
		} else if (e.getSource() == startEndVoteButton) {
			if (startEndVoteButton.getText().equals("Start vote")) {
				try {
					Socket connectionSocket = new Socket("127.0.0.1", 4444);
					ObjectOutputStream objectOut = new ObjectOutputStream(
							connectionSocket.getOutputStream());
					ObjectInputStream objectIn = new ObjectInputStream(
							connectionSocket.getInputStream());

					AdminControlMessage adminControlMessage = new AdminControlMessage();
					adminControlMessage.setMessage("Start the voting process");
					adminControlMessage.setUserSession(this.frame
							.getUserSession());
					objectOut.writeObject(adminControlMessage);
					objectOut.flush();
					String message = (String) objectIn.readObject();
					if (message.equals("Voting has started")) {
						JOptionPane.showMessageDialog(this,
								"Voting has started!");
						this.startEndVoteButton.setText("End vote");
						this.addButton.setEnabled(false);
						this.newCandidateTextField.setEnabled(false);
					}

				} catch (Exception ex) {
					ex.printStackTrace();
				}

			} else if (startEndVoteButton.getText().equals("End vote")) {
				try {
					Socket connectionSocket = new Socket("127.0.0.1", 4444);
					ObjectOutputStream objectOut = new ObjectOutputStream(
							connectionSocket.getOutputStream());
					ObjectInputStream objectIn = new ObjectInputStream(
							connectionSocket.getInputStream());

					AdminControlMessage adminControlMessage = new AdminControlMessage();
					adminControlMessage.setMessage("End the voting process");
					adminControlMessage.setUserSession(this.frame
							.getUserSession());
					objectOut.writeObject(adminControlMessage);
					objectOut.flush();
					String message = (String) objectIn.readObject();
					if (message.equals("Voting has ended")) {
						JOptionPane.showMessageDialog(this, "Voting has ended");
						this.frame.goToViewResultsPanel();
					}

				} catch (Exception ex) {
					ex.printStackTrace();
				}
			}

		}

	}
}
